import axios from "axios"

export default {
    namespaced: true,
    state: {
        pageData:{
            currentPage: 1,
            pageSize: 5,
            totalPage: 1,
            totalCount: 1,
            order: []
        },
        choice: "",
        searchFor: "",
        isModified: "",
    },
    getters: {
        newPageData(state){
            if(state.searchFor){
                return {...state.pageData, order: state.pageData.order.filter(items => items[state.choice].includes(state.searchFor))};
            }
            return state.pageData
        }
    },
    mutations: {
        getOrderWaiting(state, payload){
            state.pageData = payload
        },
        deliveryAsync(state, payload){
            state.isModified = payload;
        },
        searchingFor(state, payload){
            state.choice = payload.choice;
            state.searchFor = payload.searchFor;
        },
        getAllOrde(state, payload){
            state.pageData = payload
        }
    },
    actions: {
        getOrderWaitingAsync(context){
            axios.get("/api/orders/getOrderWaiting", {params: context.state.pageData}).then(res => {
                context.commit("getOrderWaiting", res.data);
            })
        },
        getAllOrderAsync(context){
            axios.get("/api/orders/getAllOrder", {params: context.state.pageData}).then(res => {
                context.commit("getAllOrde", res.data);
            })
        },
        deliveryAsync(context, payload){
            axios.post("/api/orders/modifyOrderStatus", {_id: payload._id}).then(res => {
                context.commit("deliveryAsync", res.data);
            })
        }
    },
    modules: {
    }
}